package com.qersoft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qersoft.mapper.ExamPaperMapper;
import com.qersoft.dao.ExamRuleMapper;
import com.qersoft.dao.QuestionBankMapper;
import com.qersoft.entity.ExamPaper;
import com.qersoft.entity.ExamRule;
import com.qersoft.entity.QuestionBank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service("ExamPaperService")
public class ExamPaperServiceImpl extends ServiceImpl<ExamPaperMapper, ExamPaper>
        implements ExamPaperService {
    
    @Autowired
    private ExamPaperMapper examPaperMapper;
    
    @Autowired
    private ExamRuleMapper examRuleMapper;
    
    @Autowired
    private QuestionBankMapper questionBankMapper;

    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper) {
        this.examPaperMapper = examPaperMapper;
    }

    @Override
    public ExamPaper generateExamPaper(ExamRule rule, String paperName) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setPaperName(paperName);
        examPaper.setTotalScore(rule.getTotalScore());

        List<QuestionBank> selectedQuestions = getQuestionsByRule(rule);
        examPaper.setQuestions(selectedQuestions);

        examPaper.setCreateTime(LocalDateTime.now());
        examPaper.setUpdateTime(LocalDateTime.now());

        examPaperMapper.insert(examPaper);

        return examPaper;
    }

    @Override
    public List<ExamPaper> getExamPaperList() {
        return examPaperMapper.selectList(null);
    }

    @Override
    public ExamPaper getExamPaperById(Integer paperId) {
        return examPaperMapper.selectById(paperId);
    }

    @Override
    public ExamRule getExamRuleById(Integer ruleId) {
        return examRuleMapper.selectById(ruleId);
    }

    @Override
    public List<QuestionBank> getQuestionsByRule(ExamRule rule) {
        QueryWrapper<QuestionBank> wrapper = new QueryWrapper<>();
        if (rule != null) {
            if (rule.getType() != null) {
                wrapper.eq("type", rule.getType());
            }
            if (rule.getDifficulty() != null) {
                wrapper.eq("difficulty", rule.getDifficulty());
            }
            if (rule.getSubjectId() != null) {
                wrapper.eq("subject_id", rule.getSubjectId());
            }
            if (rule.getQuestionCount() != null) {
                wrapper.last("LIMIT " + rule.getQuestionCount());
            }
        }
        return questionBankMapper.selectList(wrapper);
    }

    @Override
    public String add(Long userId, Long paperId) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);
        
        if (examPaper == null) {
            return "error";
        } else {
            if(examPaper.getPaperIds() == null) {
                examPaper.setPaperIds(paperId + ",");
                examPaperMapper.updateById(examPaper);
                return "success";
            } else {
                String ids = examPaper.getPaperIds();
                ids += paperId + ",";
                examPaper.setPaperIds(ids);
                examPaperMapper.updateById(examPaper);
                return "success";
            }
        }
    }

    @Override
    public String del(Long userId, Long paperId) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        ExamPaper examPaper = examPaperMapper.selectOne(queryWrapper);
        
        if (examPaper == null) {
            return "error";
        } else {
            if(examPaper.getPaperIds() == null) {
                return "error";
            } else {
                String ids = examPaper.getPaperIds();
                ids = ids.replace(paperId + ",", "");
                examPaper.setPaperIds(ids);
                examPaperMapper.updateById(examPaper);
                return "success";
            }
        }
    }

    @Override
    public List<ExamPaper> selectByRuleId(Integer ruleId) {
        QueryWrapper<ExamPaper> wrapper = new QueryWrapper<>();
        wrapper.eq("rule_id", ruleId);
        return examPaperMapper.selectList(wrapper);
    }
}